import React from 'react';
import {Card, InputNumber, Space} from "antd";
import {Link, useNavigate} from "react-router-dom";
import { Button, Form, Input, Radio } from 'antd';
import TextArea from "antd/es/input/TextArea";
import {SecurityUserType} from "type/SecurityUser";
import LogFactory from "log/LogFactory";
import {requestPut} from "util/request";
import GlobalHandler from "global/GlobalHandler";

const log = LogFactory.getLog("UserAddComponent") ;
const UserAddComponent = () => {
    const [form] = Form.useForm();
    const navigate = useNavigate() ;
    const onreset = () => {
        form.resetFields();
    };

    const onFinish = (values: SecurityUserType) => {
        log.debug("提交添加用户表单",values) ;
        requestPut<number>("/manager/security-user",values)
            .then(res => {
                if (res.data > 0) {
                    log.debug("添加用户数据成功") ;
                    onreset() ;
                   GlobalHandler.getInstance().sendSuccessMessage("添加用户数据成功") ;
                    navigate("../") ;
                }else {
                    log.debug("添加用户数据失败") ;
                    GlobalHandler.getInstance().sendErrorMessage("添加用户数据失败") ;
                }
            });
    };

    return (
        <Card
            title={"添加用户数据"}
            extra={ <Link key={'add-user-data'} to={'../'}>用户列表</Link>}
        >
            <Form<SecurityUserType>
                form={form}
                layout={"horizontal"}
                // labelWrap={false}
                labelCol={{ span: 5 }}
                wrapperCol={{ span: 10 }}
                onFinish={ onFinish }
                initialValues={{
                    order: 1,
                    age: 18,
                    gender: true,
                }}
            >
                <Form.Item label="用户名" name={"username"}>
                    <Input placeholder="请输入用户名" />
                </Form.Item>
                <Form.Item label="登录密码" name={"password"}>
                    <Input placeholder="请求输入用户密码" />
                </Form.Item>
                <Form.Item label="用户身份" name={"identity"}>
                    <Input placeholder="请求输入用户身份" />
                </Form.Item>
                <Form.Item name="age" label="用户年龄">
                    <InputNumber min={1} max={200}/>
                </Form.Item>
                <Form.Item label="用户性别" name="gender">
                <Radio.Group>
                    <Radio.Button value={true}>男</Radio.Button>
                    <Radio.Button value={false}>女</Radio.Button>
                </Radio.Group>
                </Form.Item>
                <Form.Item name="order" label="数据排序">
                    <InputNumber min={0}/>
                </Form.Item>
                <Form.Item label="备注信息" name={"remark"}>
                    <TextArea rows={3} />
                </Form.Item>
                <Form.Item wrapperCol={{offset: 8,span:16}}>
                    <Space>
                        <Button type="primary" htmlType="submit">添加</Button>
                        <Button htmlType="button" onClick={onreset} >重置</Button>
                        <Button type="link" htmlType="button">
                            填充字段
                        </Button>
                    </Space>
                </Form.Item>
            </Form>
        </Card>
    );
};

export default UserAddComponent;